今天來多寫另外三個功能 刪除 更新 查詢
\\刪除對應名字的數據
public long deleteByName (String name){
return mWDB.delete(TABLE_NAME,"name=?",new String[]{name});
}
\\利用名字上傳新的數據
public long update(User user){
ContentValues values = new ContentValues();
values.put("name", user.name);
values.put("age", user.age);
values.put("height", user.height);
values.put("weight", user.weight);
values.put("married", user.married);
return mWDB.update(TABLE_NAME,values,"name=?",new String[]{user.name});
}
\\全查
public List<User> queryAll(){
List<User> list = new ArrayList<>();
Cursor cursor = mRDB.query(TABLE_NAME,null,null,null,null,null,null);
while (cursor.moveToNext()){
User user = new User();
user.id = cursor.getInt(0);
user.name = cursor.getString(1);
user.age = cursor.getInt(2);
user.height = cursor.getLong(3);
user.weight = cursor.getFloat(4);
user.married = (cursor.getInt(5)==0) ? false:true;
list.add(user);
}
return list;
}
\\名字查詢
public List<User> queryByName(String name){
List<User> list = new ArrayList<>();
Cursor cursor = mRDB.query(TABLE_NAME,null,"name=?",new String[]{name},null,null,null);
while (cursor.moveToNext()){
User user = new User();
user.id = cursor.getInt(0);
user.name = cursor.getString(1);
user.age = cursor.getInt(2);
user.height = cursor.getLong(3);
user.weight = cursor.getFloat(4);
user.married = (cursor.getInt(5)==0) ? false:true;
list.add(user);
}
return list;
}